/**
 * Copyright (c) 2017-2024 Nop Platform. All rights reserved.
 * Author: canonical_entropy@163.com
 * Blog:   https://www.zhihu.com/people/canonical-entropy
 * Gitee:  https://gitee.com/canonical-entropy/nop-entropy
 * Github: https://github.com/entropy-cloud/nop-entropy
 */
package com.gitee.wsl.text.marker

import kotlin.test.Test
import kotlin.test.assertEquals

class TestMarkedString {
    @Test
    fun testTransform() {
        val sb = MarkedStringBuilder()
        sb.append("01")
        sb.appendWithValueMarker("XX", "a", "A", false)
        sb.append('4')
        sb.append("5UV")
        sb.appendMarker(ValueMarker(6, 8, "b", "B", false))

        val str: MarkedString = sb.end()
        assertEquals("01XX45UV", str.text)
        assertEquals(2, str.markers[0].textBegin)
        assertEquals(4, str.markers[0].textEnd)

        println(str.dumpText)

        val sb2: MarkedStringBuilder = MarkedStringBuilder()
        sb2.appendWithTransform(sb) { marker ->
            if (marker.textBegin == 2) return@appendWithTransform null
            MarkedStringBuilder().append("abc")
        }

        assertEquals("01XX45abc", sb2.text)
        assertEquals(1, sb2.markers.size)
        assertEquals(2, sb2.markers[0].textBegin)
    }

    @Test
    fun testTransformFirst() {
        val sb = MarkedStringBuilder()
        sb.append("01")
        sb.appendWithValueMarker("XX", "a", "A", false)
        sb.append('4')
        sb.append("5UVW")
        sb.appendMarker(ValueMarker(6, 8, "b", "B", false))

        val str: MarkedString = sb.end()
        assertEquals("01XX45UVW", str.text)
        assertEquals(2, str.markers[0].textBegin)
        assertEquals(4, str.markers[0].textEnd)

        println(str.dumpText)

        val sb2 = MarkedStringBuilder()
        sb2.appendWithTransform(sb) { marker ->
            if (marker.textBegin == 6) return@appendWithTransform null
            MarkedStringBuilder().append("abc")
        }

        assertEquals("01abc45UVW", sb2.text)
        assertEquals(1, sb2.markers.size)
        assertEquals(7, sb2.markers[0].textBegin)
    }
}